package com.sskj.mine.asset;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.NumberUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.RecordsBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/18
 */
@Route(path = RConfig.MINE_FRAGMENT_ASSET)
public class AssetFragment extends BaseFragment<AssetPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    //  2 提币 1 充币
    @Autowired
    int type;
    @Autowired
    String code;
    private MVCCoolHelper<List<RecordsBean>> mvcHelper;

    @Override
    public int getLayoutId() {
        return R.layout.mine_fragment_asset;
    }

    @Override
    public AssetPresenter getPresenter() {
        return new AssetPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        HashMap<Integer, String> tibiMap = new HashMap<>();
        tibiMap.put(0, App.INSTANCE.getString(R.string.mineassetFragment1));
        tibiMap.put(1, App.INSTANCE.getString(R.string.mineassetFragment2));
        tibiMap.put(2, App.INSTANCE.getString(R.string.mineassetFragment3));

        HashMap<Integer, String> chongbiMap = new HashMap<>();
        chongbiMap.put(0, App.INSTANCE.getString(R.string.mineassetFragment4));
        chongbiMap.put(1, App.INSTANCE.getString(R.string.mineassetFragment5));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.lib_divider))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.mine_item_asset_records, new SlimInjector<RecordsBean>() {
            @Override
            public void onInject(RecordsBean data, IViewInjector injector, List<Object> list) {

                injector
                        .text(R.id.item_address_title, type == 2 ? App.INSTANCE.getString(R.string.mineaddAddressActivity1) : App.INSTANCE.getString(R.string.mineassetFragment7))
                        .text(R.id.tv_time_des, type == 2 ? App.INSTANCE.getString(R.string.mineassetFragment8) : App.INSTANCE.getString(R.string.mineassetFragment9))
                        .text(R.id.item_num_title, type == 2 ? App.INSTANCE.getString(R.string.mineassetFragment10) : App.INSTANCE.getString(R.string.mineassetFragment11))
//                        .text(R.id.tvStatus, type == 2 ? tibiMap.get(data.getInspectStatus()) : chongbiMap.get(data.getInspectStatus()))
                        .text(R.id.item_time2, type == 2 ? data.getCreateTime() : data.getCreateTime())
                        .visibility(R.id.llCheck, type == 2 ? View.VISIBLE : View.GONE)
                        .visibility(R.id.tvStatus, type == 2 ? View.VISIBLE : View.GONE)
                        .text(R.id.item_address, data.getWalletAddr())
                        .text(R.id.item_num, NumberUtil.keep(data.getUsdFee(), 8, true) + data.getCode());
//                        .clicked(R.id.tvCancel, v -> {
//                            TipUtil.getSureCancelTip(getActivity(), null, App.INSTANCE.getString(R.string.mineassetFragment12), () -> {
//                                mPresenter.cancelTibi(data.getId());
//                            });
//                        })
//                        .text(R.id.tvCreateTime, data.getCreateTime());
                if (type == 2) {
                    injector.text(R.id.item_review_time, data.getInspectTime());
                    injector.text(R.id.tv_status, tibiMap.get(Integer.valueOf(data.getInspectStatus())));
                    if ("0".equals(data.getInspectStatus())) {
                        injector.textColor(R.id.tv_status, getResources().getColor(R.color.lib_dominant));
                    } else if ("1".equals(data.getInspectStatus())) {
                        injector.textColor(R.id.tv_status, getResources().getColor(R.color.libUpColor));
                    } else if ("2".equals(data.getInspectStatus())) {
                        injector.textColor(R.id.tv_status, getResources().getColor(R.color.libDownColor));
                    }

                }
            }

        }).attachTo(recyclerView);
        slimAdapter.updateData(new ArrayList());
        ModelRx2DataSource<RecordsBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<RecordsBean>)
                page -> mPresenter.getAssetRecord(code, type + "", page + "", "20"), 20);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    public void cancelSuccess() {
        mvcHelper.refresh();
    }
}
